<template>
  <!-- 密码修改页 -->

  <div class="maxBox">
     <!-- 标题组件 -->
      <panel title="修改密码">
      
      <el-form ref="form" :model="formData" label-width="150px">
				<el-form-item label="旧密码">
				  <el-input v-model="formData.oldPwd"></el-input>
				</el-form-item>
				<el-form-item label="新密码">
					<el-input v-model="formData.newPwd"></el-input>
				  </el-form-item>
			
				  <el-form-item label="确认新密码">
					<el-input v-model="formData.newPwd1"></el-input>
				  </el-form-item>
			  
			
				<el-form-item>
				  <el-button type="primary" @click="updateBtn()">确认</el-button>
				  <el-button @click="newEdit()">重置</el-button>
				</el-form-item>
			  </el-form>
      </panel>

  </div>
</template>

<script>
// 导入公共标题组件
import Panel from "../../components/Panel.vue";
import {editPwd} from '@/api/user.js'

export default {
  data() {
    return {
      formData:{
          newPwd:'',
          oldPwd:'',
          newPwd1:'',
      },
    };
  },
    // 注册组件
  components: {
    Panel,
  },

  // 方法
  methods:{
    //修改信息
    async updateBtn(){

			if(this.formData.newPwd === this.formData.newPwd1){
				//如果旧密码和新密码相等可以请求接口
				let {id} = JSON.parse(localStorage.getItem('user'));
				//组装后端需要的数据
				let data = {id, newPwd:this.formData.newPwd, oldPwd:this.formData.oldPwd,}
				console.log(data);

        // 如果成功code=0 提示用户
				let res = await editPwd(data);
				if(res.data.code ===0){
					this.$message({
						type:'success',
						message:res.data.msg,
					});

					//修改密码成功 清楚本地缓存并重新登录
          setInterval(() => {
            localStorage.clear();
					  this.$router.push('/login');
          }, 1000);
					
          }else{
            this.$message.error(res.data.msg);
          };
          // 否则就提示用户
          }else{
          this.$message.error('两次密码不一致');
        };
		},

    // 重置按钮 点击重置清空数据
    newEdit(){
      this.formData.newPwd="";
      this.formData.oldPwd="";
      this.formData.newPwd1="";
      console.log("重置清空");
    },
  },
};
</script>

<style lang="less" scoped>
.maxBox {
  width: 100%;
  height: 100%;
  .contentBox {
    margin: 15px auto 0 auto;
    height: 400px;
    width: 97%;
    background: #fff;
    .title {
      width: 100%;
      height: 50px;
      line-height: 50px;
      padding-left: 15px;
      border-bottom: 2px solid rgb(233, 233, 233);
    }
  }
}
.oldPwd {
  display: flex;
  margin-left: 75px;
  width: 330px;
  line-height: 66px;
  p {
    width: 99px;
    line-height: 66px;
  }
}
.newPwd {
  display: flex;
  margin-left: 75px;
  margin-top: 5px;
  width: 330px;
  line-height: 66px;
  p {
    width: 99px;
    line-height: 66px;
  }
}
.ConfirmPwd {
  display: flex;
  margin-left: 51px;
  margin-top: 5px;
  width: 353px;
  line-height: 66px;
  p {
    width: 140px;
    line-height: 66px;
  }
}
.Btn {
  margin-top: 20px;
  margin-left: 150px;
}
.el-input{
  width: 30%;
}
</style>